﻿using System;
using MyTinnyApi.model;

namespace MyTinnyApi.util
{
	public class EnvUtil
	{
		/// <summary>
		/// 获取所有环境变量
		/// </summary>
		/// <returns></returns>
		public static List<Env> GetAllEnv()
		{
			var envList = DbUtil.ExecuteReaderForType<Env>("select * from t_env");
			var envDetailList = DbUtil.ExecuteReaderForType<EnvDetail>("select * from t_env_detail");
			envList.ForEach(env =>
			{
				env.Values = envDetailList.Where(d => d.EnvId == env.Id).ToList();
			});

			return envList;
		}

		public static int InsertEnv(Env env)
		{
			int id = -1;
            using (var db = new AppDbContext())
            {
                //db.Database.EnsureCreated();
                db.Env.Add(env);
                db.SaveChanges();

                id = env.Id;
				env.Values?.ForEach(d =>
				{
					d.EnvId = id;
					db.EnvDetail.Add(d);
				});

                db.SaveChanges();
            }

			return id;
        }
	}
}

